package org.example.Polls;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddParticipantsActivity extends ListActivity {
	// Use for Logs:
	private static final String TAG = "AddParticipantsActivity";
	
	// Used for returning data back to calling activity.
	public static final String ADD_PARTICIPANTS_EXTRA = "uVote.AddParticipantsActivity.participants";

	// List of array strings which will serve as list items.
	ArrayList<String> listItems = new ArrayList<String>();

	// Defining string adapter which will handle data of listview.
	ArrayAdapter<String> adapter;

	// Recording how much times button was clicked.
	int clickCounter = 0;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.add_participants);
		Button doneBtn = (Button)findViewById(R.id.doneBtn);
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		listItems = (ArrayList) extras.getSerializable(ADD_PARTICIPANTS_EXTRA);
		// Finish the activity when the "Done" button is pressed
		doneBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = getIntent();
				intent.putExtra(ADD_PARTICIPANTS_EXTRA, listItems);
				setResult(RESULT_OK, intent);
				AddParticipantsActivity.this.finish();
			}
		});
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
		setListAdapter(adapter);
	}

	// Method which will handle dynamic insertion
	public void addItems(View v) {
		showDialog(R.id.layout_root);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.custom_add_participant_dialog);
		dialog.setTitle("Add Participant by E-mail");
		
		final EditText eText = (EditText) dialog.findViewById(R.id.text_email);
		Button dialogAdd = (Button) dialog.findViewById(R.id.dialogadd);
		dialogAdd.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(eText.getText().toString().length() != 0) {
					Log.d(TAG, "Adding participant to list: "+eText.getText().toString());
					listItems.add(eText.getText().toString());
				}
				adapter.notifyDataSetChanged();
				dialog.dismiss();
			}
		});
		Button dialogCancel = (Button) dialog.findViewById(R.id.dialogcancel);
		dialogCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		//dialog.show();
		return dialog;
	}
}